<?php
/**
 * Created by PhpStorm.
 * User: 程序猴
 * Date: 2018/4/9
 *  Q Q: 505413
 * Time: 10:08
 */

namespace app\admin\controller;
use app\admin\controller\Base;
use app\admin\model\UserModel;
use app\admin\model\UserType;
use think\Db;
class User extends Base
{
    //用户列表
    public function index()
    {
        $key = input('key');
        $map = [];
        if($key&&$key!=="")
        {
            $map['username'] = ['like',"%" . $key . "%"];
        }
        $lst = Db::name('admin')
            ->alias('a')
            ->field('a.id,username,loginnum,last_login_time,a.status,groupid,title,last_login_ip')
            ->join('auth_group b ','a.groupid= b.id')
            ->where($map)
            ->paginate(10);
        $this->assign('lst',$lst);
        return $this->fetch();
    }
    //修改列表的开启状态
    public function status_edit()
    {
        $id = input('param.id');
        $status = Db::name('admin')->where('id',$id)->value('status');//判断当前状态情况
        if($status==1)
        {
            $flag = Db::name('admin')->where('id',$id)->setField(['status'=>0]);
            return json(['code' => 1, 'data' => $flag['data'], 'msg' => '已禁止']);
        }
        else
        {
            $flag = Db::name('admin')->where('id',$id)->setField(['status'=>1]);
            return json(['code' => 0, 'data' => $flag['data'], 'msg' => '已开启']);
        }

    }
    //添加用户
    public function userAdd()
    {
        if(request()->isAjax()){
            $param = input('post.');
            if(!empty($param['status'])){
                if($param['status'] == 'on') {
                    $param['status'] = '1';
                }
            }else{
                $param['status'] = '0';
            }
            $param['password'] = md5($param['password']);
            $user = new UserModel();
            $flag = $user->insertUser($param);
            $accdata = array(
                'uid'=> $user['id'],
                'group_id'=> $param['groupid'],
            );
            $group_access = Db::name('auth_group_access')->insert($accdata);
            return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
        }

        //管理员角色展示
        $authGroupRes=db('auth_group')->select();
        $this->assign('authGroupRes',$authGroupRes);
//        $role = new UserType();
//        $this->assign('group',$role->getRole());
        return $this->fetch();
    }
    //修改管理员
    public function userEdit()
    {
        $user = new UserModel();
        if(request()->isAjax()){
            $param = input('post.');

            if(!empty($param['status'])){
                if($param['status'] == 'on') {
                    $param['status'] = '1';
                }
            }else{
                $param['status'] = '0';
            }

            if(empty($param['password'])){
                unset($param['password']);
            }else{
                $param['password'] = md5($param['password']);
            }
            $flag = $user->editUser($param);
            $group_access = Db::name('auth_group_access')->where('uid', $user['id'])->update(['group_id' => $param['groupid']]);
            if($flag && $group_access){
                return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
            }else{
                return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
            }
        }
        $id = input('param.id');
        $role = new UserType();
        $this->assign([
            'user' => $user->getOneUser($id),
            'group' => $role->getRole()
        ]);
        $authGroupAccess=db('auth_group_access')->where(array('uid'=>$id))->find();
        $authGroupRes=db('auth_group')->select();
        $this->assign('authGroupRes',$authGroupRes);
        $this->assign('groupId',$authGroupAccess['group_id']);
        return $this->fetch();
    }
    //删除管理员
    public function userDel()
    {
        $id = input('id');
        $res = Db::name('admin')->where(array('id'=>$id))->delete();
        if($res){
            return json(['code' => 1, 'url' => 'reduction/index', 'msg' => '删除用户成功']);
        }else{
            return json(['code' => -1, 'url' => 'reduction/index', 'msg' => '删除用户，请重试']);
        }
    }

}